Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce 'traverse' library #117

Merged
merged 2 commits into from
Jun 12, 2022
Merged

feat: Introduce 'traverse' library #117

merged 2 commits into from
Jun 12, 2022

Conversation

scarmuega
Copy link
Member

@scarmuega scarmuega commented Jun 12, 2022

Traversing over block data is a very common task and, at the same time, more complex that it should be. Reasons are:

  • Dealing with era-specific details adds a lot of branching in consumer code
  • Block data layout makes it harder to iterate over independent transactions

The new pallas-traverse lib attempts to abstract away that complexity by creating a multi-era layer on top of the ledger primitives. By providing a MultiEraBlock and a MultiEraTx, the lib consumer can code against a single structure regardless of the era.

This is an example of how to iterate over Tx of blocks from multiple eras (see `examples/block-decode):

use pallas::ledger::traverse::MultiEraBlock;

fn main() {
    let blocks = vec![
        include_str!("blocks/byron.block"),
        include_str!("blocks/shelley.block"),
        include_str!("blocks/mary.block"),
        include_str!("blocks/allegra.block"),
        include_str!("blocks/alonzo.block"),
    ];

    for block_str in blocks.iter() {
        let cbor = hex::decode(block_str).expect("invalid hex");

        let block = MultiEraBlock::decode(&cbor).expect("invalid cbor");

        println!("{} {}", block.slot(), block.hash());

        for tx in block.tx_iter() {
            println!("{:?}", tx);
        }
    }
}

The api surface of the lib is still limited, but the goal is to evolve it so that it becomes a single point of entry for "traversing" over any block data. At some point, this will be a replacement of the ad-hoc "crawling" mechanism that we have in Oura.

@scarmuega scarmuega marked this pull request as ready for review June 12, 2022 21:45
@scarmuega scarmuega merged commit 26da913 into main Jun 12, 2022
@scarmuega scarmuega deleted the feat/traverse branch July 2, 2022 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant